package com.dqyz.web.controller.busi.wechat;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
import cn.hutool.core.util.RandomUtil;
import com.dqyz.busi.domain.Member;
import com.dqyz.busi.domain.vo.WxMiniLoginReqVo;
import com.dqyz.busi.domain.vo.WxMiniLoginRespVo;
import com.dqyz.busi.service.IMemberService;
import com.dqyz.common.annotation.NeedWechatLogin;
import com.dqyz.common.config.WxMaConfiguration;
import com.dqyz.common.config.WxMaProperties;
import com.dqyz.common.core.domain.AjaxResult;
import com.dqyz.common.core.redis.RedisCache;
import com.dqyz.common.utils.StringUtils;
import com.dqyz.framework.aspectj.NeedWechatLoginAspect;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * 微信小程序登陆Controller
 *
 * @author wangguan
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/wechat")
public class WxLoginController {

    @Autowired
    private IMemberService memberService;

    @Autowired
    private  WxMaService wxMaService;

    @Autowired
    private WxMaProperties wxMaProperties;
    @Autowired
    private RedisCache redisCache;

    @PostMapping("/weixin-mini-app-login")
    public AjaxResult wxLogin(@RequestBody WxMiniLoginReqVo wxMiniLoginReqVo) throws WxErrorException {
        if(StringUtils.isBlank(wxMiniLoginReqVo.getLoginCode())) {
            return AjaxResult.error(500,"登录码不能为空");
        }
        try {
            WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(wxMiniLoginReqVo.getLoginCode());
            String openid = session.getOpenid();
            System.out.println("openid:"+openid);

            WxMaPhoneNumberInfo phoneNumberInfo = wxMaService.getUserService().getPhoneNoInfo(wxMiniLoginReqVo.getPhoneCode());
            String phoneNumber = phoneNumberInfo.getPhoneNumber();
            System.out.println("phoneNumber:"+phoneNumber);

            // 获取登录用户
            Member user = memberService.registAndLogin(openid, phoneNumber);
            // 生成wxToken
            String wxToken = "wxtoken:"+ RandomUtil.randomString(10);
            // 保存登录信息到redis
            redisCache.setCacheObject(wxToken, user, 30, TimeUnit.MINUTES);

            WxMiniLoginRespVo respVo = new WxMiniLoginRespVo();
            respVo.setUserId(user.getId());
            respVo.setWxToken(wxToken);
            return AjaxResult.success("登录成功", respVo);
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw e;
        } finally {
            WxMaConfigHolder.remove();
        }
    }

    // 获取用户信息接口
    @GetMapping("/getInfo")
    @NeedWechatLogin
    public AjaxResult getUserInfo(HttpServletRequest req) {
//        String wxToken = req.getHeader("wxToken");
//        Member user = redisCache.getCacheObject(wxToken);
//        Long id = user.getId();
//        Member member = memberService.selectMemberById(id);

        Member member = NeedWechatLoginAspect.wechatLoginThreadLocal.get();
        return AjaxResult.success(member);
    }

}
